
package CtrlLayer;
import ModelLayer.*;
import DBLayer.*;

import java.util.ArrayList;

/**
 * @version 12/22/2011
 * @author Gruppe 4
 */

public class CtrlPerson 
{
    private IFDBPerson dbPerson;
    
    //Creates a new instance of CtrlPerson
    public CtrlPerson()
    {
        dbPerson = new DBPerson();
    }
    
    //Calls DBPerson to get all employees
    public ArrayList<Person> findAllEmployees()
    {
        return dbPerson.getAllEmployees();  
    }
    
    /**
     * Calls DBOrder to get all orders of a person (should call CtrlOrder)
     * @param personId Id of the person
     * @param retrieveAssociation
     * @return ArrayList<Order> ArrayList of orders
     */
     public ArrayList<Order> findOrders(int personId, boolean retrieveAssociation)
     {
         //should call CtrlOrder instead
         IFDBOrder dbOrder = new DBOrder();
         ArrayList<Order> result = dbOrder.findOrders(personId, retrieveAssociation);
         return result;
     }
      
     /**
      * Calls DBPerson to find a person
      * @param personId The id of the person
      * @param isEmployee True: The person is an employee false: a customer
      * @return ArrayList<Person> ArrayList of persons
      */
     public ArrayList<Person> findPersonsById(int personId, boolean isEmployee)
     {
         // should get retrieveassociation from gui-call
         return dbPerson.findPersonsId(personId, false, isEmployee);
     }
     
     /**
      * Calls DBPerson to get all persons with the specified first name
      * @param fName First name of person
      * @param isEmployee True: The person is an employee false: a customer
      * @return ArrayList<Person> ArrayList of persons
      */
     public ArrayList<Person> findPersonsfName(String fName, boolean isEmployee)
     {
          return dbPerson.findPersonsfName(fName, false, isEmployee);
     }
     
     /**
      * Calls DBPerson to get all persons with the specified last name
      * @param lName Last name of person
      * @param isEmployee True: The person is an employee false: a customer
      * @return ArrayList<Person> Arraylist of persons
      */
     public ArrayList<Person> findPersonslName(String lName, boolean isEmployee)
     {
          return dbPerson.findPersonslName(lName, false, isEmployee);
     }
      
     /**
      * Calls DBPerson to get a person with the specified id
      * @param personId Id of the person
      * @param isEmployee True: The person is an employee false: a customer
      * @return Person The person with the specified id
      */
     public Person findPersonById(int personId, boolean isEmployee)
     {
          return dbPerson.findPerson(personId, false, isEmployee);
     }

//     public ArrayList<Person> findPersonBylName(String lName, boolean isEmployee)
//     {
//          return dbPerson.findPersonslName(lName, false, isEmployee);
//     }
      
     /**
      * The method calls DBPerson to insert a new employee
      * @param fName First name of the employee
      * @param lName Last name of the employee
      * @param address Address of the employee
      * @param zipCode ZipCode of the employee
      * @param phoneNo PhoneNo of the employee
      * @param mobileNo MobileNo of the employee
      * @param email Email of the employee
      * @param accountInfo Account info for the employee
      * @param typeNo Type identification on giro
      * @param kreNo Kreditor identification on giro
      * @param cusNo Customer identification on giro
      * @param intHWage The internal hourly wage of the employee
      * @param extHWage The external hourly wage of the employee
      * @param comment A comment about the employee
      */
     public void insertNewEmployee(String fName, String lName, String address, int zipCode, 
              String phoneNo, String mobileNo, String email, String accountInfo, String typeNo, String kreNo, String cusNo, 
              double intHWage, double extHWage, String comment)
     {    
           Employee empObj = new Employee();
           empObj.setfName(fName);
           empObj.setlName(lName);
           empObj.setAddress(address);
           empObj.setZipCode(zipCode);
           empObj.setPhoneNumber(phoneNo); 
           empObj.setMobileNumber(mobileNo);
           empObj.setEmail(email);
           empObj.setAccountInfo(accountInfo);
           empObj.setTypeNo(typeNo);
           empObj.setKreNo(kreNo);
           empObj.setCusNo(cusNo);
           empObj.setInternalHourlyWage(intHWage);
           empObj.setExternalHourlyWage(extHWage);
           empObj.setComment(comment);
           
           DBConnection con = DBConnection.getInstance();
           try
           {
                DBConnection.startTransaction();
                dbPerson.insertPerson(empObj);
                DBConnection.commitTransaction();
           }
           catch(Exception e)
           {
                DBConnection.rollbackTransaction();
           }
      }
      
     /**
      * The method calls DBPerson to insert a new customer
      * @param fName First name of the customer
      * @param lName Last name of the customer
      * @param address Address of the customer
      * @param zipCode ZipCode of the customer
      * @param phoneNo PhoneNo of the customer
      * @param mobileNo MobileNo of the customer
      * @param email Email of the customer
      */
      public void insertNewCustomer(String fName, String lName, String address, int zipCode, String city, 
              String phoneNo, String mobileNo, String email)
      {    
           Customer cusObj = new Customer();
           cusObj.setfName(fName);
           cusObj.setlName(lName);
           cusObj.setAddress(address);
           cusObj.setZipCode(zipCode);
           cusObj.setCity(city);
           cusObj.setPhoneNumber(phoneNo); 
           cusObj.setEmail(email);
           
           DBConnection con = DBConnection.getInstance();
           try
           {
                DBConnection.startTransaction();
                dbPerson.insertPerson(cusObj);
                DBConnection.commitTransaction();
           }
           catch(Exception e)
           {
                DBConnection.rollbackTransaction();
                System.out.println("Transaction has been rolled back");
                System.out.println(e.getMessage());
           }
      }
      
      /**
       * The method calls DBPerson to update information regarding an employee
       * @param employeeId
       * @param fName
       * @param lName
       * @param address
       * @param zipCode
       * @param phoneNo
       * @param mobileNo
       * @param email
       * @param accountInfo
       * @param typeNo
       * @param kreNo
       * @param cusNo
       * @param intHWage
       * @param extHWage
       * @param comment
       * @return 
       */
      public int updateEmp(int employeeId, String fName, String lName, String address, int zipCode, 
              String phoneNo, String mobileNo, String email, String accountInfo, String typeNo, String kreNo, String cusNo, 
              double intHWage, double extHWage, String comment)
      {
           Employee empObj = new Employee();
           empObj.setPersonId(employeeId);
           empObj.setfName(fName);
           empObj.setlName(lName);
           empObj.setAddress(address);
           empObj.setZipCode(zipCode);
           empObj.setPhoneNumber(phoneNo); 
           empObj.setMobileNumber(mobileNo);
           empObj.setEmail(email);
           empObj.setAccountInfo(accountInfo);
           empObj.setTypeNo(typeNo);
           empObj.setKreNo(kreNo);
           empObj.setCusNo(cusNo);
           empObj.setInternalHourlyWage(intHWage);
           empObj.setExternalHourlyWage(extHWage);
           empObj.setComment(comment);
           return dbPerson.updatePerson(empObj);     
      }
}